package com.huizluo.module4.student;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class BaseCommand extends Command {

    private HashMap<String,String> args = new HashMap<>();//参数 name=>"jack"
    private HashMap<String, Command> child = new HashMap<>();//子命令 name => command

    public BaseCommand(String commandName, String commandDesc) {
        super(commandName,commandDesc);
    }

    @Override
    public void notise() {

    }

    private void parseArgs(String s) throws Exception {
        String[] strArr = s.split(" ");
        for (int i=0;i<strArr.length;i++){
            String[] kv = strArr[i].split("=");
            if (kv.length != 2){
                throw new Exception("输入有误，请重新输入");
            }
            this.args.put(kv[0].trim(),kv[1].trim());
        }
    }

    public void register(Command cmd){
        this.child.put(cmd.commandName,cmd);
    }

    private void showHelp(){
        System.out.println(this.commandDesc);
        System.out.println("help" + "           " +"输入help获取帮助");
        System.out.println("exit" + "           " +"输入exit退出");
        Set<String> strings = this.child.keySet();
        for (String s : strings){
            System.out.println(this.child.get(s).commandName + "         " + this.child.get(s).commandDesc);
        }
    }

    @Override
    public void run(HashMap<String, String> args) {
        Command current = null;
        this.showHelp();
        Scanner scan = new Scanner(System.in);
       while (true){
           String input = scan.nextLine().trim();
           if (!this.checkInput(input)){
               break;
           }

           if (current == null){
               Command cmd = this.child.get(input);
               if (cmd == null){
                   System.out.println("输入有误，请重新输入");
                   continue;
               }
               current = cmd;
               current.notise();
           }

           if (current.commandName.equals("list")){
               try {
                   current.run(this.args);
               }catch (Exception e){
                   System.out.println(e.getMessage());
               }
               current = null;
               continue;
           }

           while (true){
               String input2 = scan.nextLine().trim();
               if (!this.checkInput(input2)){
                   this.showHelp();
                   current = null;
                   break;
               }

               try {
                   this.parseArgs(input2);
               }catch (Exception e){
                   System.out.println(e.getMessage());
                   continue;
               }

               try {
                   current.run(this.args);
               }catch (Exception e){
                   System.out.println(e.getMessage());
               }
           }
       }
    }

    private boolean checkInput(String input){
        if (input.equals("help")){
            this.showHelp();
            return true;
        }

        if (input.equals("exit")){
            return false;
        }
        return true;
    }
}
